import { getMenuByPath } from "@/router/utils";
import { ref, onMounted } from "vue";

export function useMenu(powerKeys: string[]) {
  const power = ref<Record<string, boolean>>(createObjectWithKeys(powerKeys));

  function createObjectWithKeys(keys: string[]): Record<string, boolean> {
    const obj: Record<string, boolean> = {};
    keys.forEach(key => {
      obj[key] = false;
    });
    return obj;
  }

  async function loadData() {
    try {
      const { powerKey } = await getMenuByPath();
      for (const key of powerKeys) {
        power.value[key] = powerKey.includes(key);
        console.log(powerKey, key);
      }
      console.log("所有权限", powerKeys, "已有权限", powerKey);
    } catch (error) {
      console.log(error);
    }
  }

  onMounted(() => {
    loadData();
  });
  return power;
}
